<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>我是一个 iframe 页面</title>
    <style>
      .msg-box {
        display: flex;
      }
      .inline-block {
        width: 50%;
      }
      #receive_msg {
        max-height: 100px;
        overflow: auto;
      }
    </style>
  </head>
  <body>
    <p>子应用页面</p>
    <div class="msg-box">
      <div class="inline-block">
        <label for="send_msg">发送消息</label><br />
        <textarea name="send_msg" id="send_msg"></textarea><br />
        <button id="send_btn">发送</button>
      </div>
      <div class="inline-block">
        <label for="receive_msg">接收消息</label><br />
        <div id="receive_msg"></div>
      </div>
    </div>
    <script>
      send_btn.onclick = () => window.parent.postMessage(send_msg.value, '*')
      window.onmessage = e => {
        console.log('子应用接收：', e)
        receive_msg.innerHTML += `<div>${e.data || ''}</div>`
      }
    </script>
  </body>
</html>
